package com.xckj.teacher.presenter

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.blankj.utilcode.util.ToastUtils
import com.xckj.teacher.bean.AboutBean
import com.xckj.teacher.bean.BaseBean
import com.xckj.teacher.bean.LoginBean
import com.xckj.teacher.bean.LoginParam
import com.xckj.teacher.bean.User
import com.xckj.teacher.framework.IBaseView
import com.xckj.teacher.framework.net.RetrofitManager
import com.xckj.teacher.global.Constant
import com.xckj.teacher.iview.ILoginView
import com.xckj.teacher.ui.appViewModel
import com.xckj.teacher.util.CacheUtil
import okhttp3.ResponseBody
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import java.util.Objects
import java.util.Random
import kotlin.math.log

class LoginPresenter(private val loginView: IBaseView) {
    /**
     * 17634547883
     * 112233
     */
    var username = MutableLiveData<String>()
    var pass = MutableLiveData<String>()
    var cap = MutableLiveData<String>()
    var random = ""

    fun login(success: (LoginBean?) -> Unit, error: () -> Unit) {
        var loginParam = LoginParam()
        loginParam.captcha = cap.value ?: ""
        loginParam.captcha_token = random
        loginParam.account = username.value ?: ""
        loginParam.password = pass.value ?: ""
        //教师==1 学生==2
        loginParam.type = "1"
        loginView.showLoading()
        val call = RetrofitManager.getInstance().service.login(loginParam)
        call.enqueue(object : Callback<BaseBean<LoginBean?>> {
            override fun onResponse(
                call: Call<BaseBean<LoginBean?>>,
                response: Response<BaseBean<LoginBean?>>
            ) {
                loginView.hideLoading()
                if (response.isSuccessful) {
                    val baseBean = response.body()!!
                    if (Constant.SUCCESS_CODE == Objects.requireNonNull(baseBean).code) {
                        CacheUtil.setToken(baseBean.data?.userinfo?.token ?: "")
                        success(baseBean.data)
                        CacheUtil.setLoginUser(User(username.value ?: "", pass.value ?: ""))
                    } else {
                        ToastUtils.showShort(baseBean.msg)
                        error()
                    }
                } else {
                    ToastUtils.showShort("请求失败")
                    error()
                }
            }

            override fun onFailure(call: Call<BaseBean<LoginBean?>>, t: Throwable) {
                t.printStackTrace()
                loginView.hideLoading()
                ToastUtils.showShort("请求失败")
                error()
            }
        })
    }

    fun getAbout() {
        loginView.showLoading()
        val call = RetrofitManager.getInstance().service.about()
        call.enqueue(object : Callback<BaseBean<AboutBean?>> {
            override fun onResponse(
                call: Call<BaseBean<AboutBean?>>,
                response: Response<BaseBean<AboutBean?>>
            ) {
                loginView.hideLoading()
                if (response.isSuccessful) {
                    val baseBean = response.body()!!
                    if (Constant.SUCCESS_CODE == Objects.requireNonNull(baseBean).code) {
                        appViewModel.about.value = baseBean.data
                    } else {
                        ToastUtils.showShort(baseBean.msg)
                    }
                } else {
                    ToastUtils.showShort("请求失败")
                }
            }

            override fun onFailure(call: Call<BaseBean<AboutBean?>>, t: Throwable) {
                t.printStackTrace()
                loginView.hideLoading()
                ToastUtils.showShort("请求失败")
            }
        })
    }

    fun getCaptcha(success: (String, ByteArray?) -> Unit) {
        random = Random().nextInt(10000000).toString()
        val call = RetrofitManager.getInstance().service.getCaptcha(random)
        call.enqueue(object : Callback<ResponseBody> {
            override fun onResponse(
                call: Call<ResponseBody>,
                response: Response<ResponseBody>
            ) {
                if (response.isSuccessful) {
                    val body: ResponseBody? = response.body()
                    if (body != null) {
                        success(random, body.bytes())
                    }
                } else {
                    ToastUtils.showShort("请求失败")
                }
            }

            override fun onFailure(call: Call<ResponseBody>, t: Throwable) {
                t.printStackTrace()
                ToastUtils.showShort("请求失败")
            }
        })
    }
}
